package com.molyfun.parents.activity.common;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import com.molyfun.parents.R;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class GameViewerActivity extends Activity {

    @InjectView(R.id.wv_game)
    WebView webView;
    @InjectView(R.id.bt_game_back)
    ImageView btBack;
    public static final String FLAG_GAME_VIEW_URL_TRANSFER = "game_url";

    public static void startActivity(Context context, String gameUrl){
        Intent intent = new Intent();
        intent.setClass(context, GameViewerActivity.class);
        intent.putExtra(FLAG_GAME_VIEW_URL_TRANSFER, gameUrl);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_viewer);
        ButterKnife.inject(this);

        Window window = getWindow();
        if (Build.VERSION.SDK_INT >= 19) {
            int newVis = View.SYSTEM_UI_FLAG_LAYOUT_STABLE

                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION

                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar

                    | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar

                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

            this.getWindow().getDecorView().setSystemUiVisibility(newVis);
        } else {
            window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }

        String url = getIntent().getStringExtra(FLAG_GAME_VIEW_URL_TRANSFER);
        webView.loadUrl(url);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setBuiltInZoomControls(true);
        webView.setWebChromeClient(new WebChromeClient());
        webView.setWebViewClient(new WebViewClient());
        btBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GameViewerActivity.this.finish();
            }
        });

    }

}